package cn.muqingfeng.bbs.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import cn.muqingfeng.bbs.R;
import cn.muqingfeng.bbs.constants.Constants;

public class WebViewFragment extends BaseFragment {
	private WebView mWebView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (mWebView == null) {
			mWebView = (WebView) inflater.inflate(R.layout.webview, null);
			WebSettings settings = mWebView.getSettings();
			settings.setJavaScriptEnabled(true);
			settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
			settings.setUseWideViewPort(true);
			mWebView.setWebViewClient(new WebViewClient() {
				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					if (!TextUtils.isEmpty(url) && (url.startsWith("http://") || url.startsWith("https://"))) {
						view.loadUrl(url);
					}
					return true;
				}
			});
			String url = getArguments().getString(Constants.EXTRA_URL);
			mWebView.loadUrl(url);
		}
		ViewGroup parent = (ViewGroup) mWebView.getParent();
		if (parent != null) {
			parent.removeView(mWebView);
		}
		return mWebView;
	}

	@Override
	public boolean onBackPressed() {
		if (mWebView.canGoBack()) {
			mWebView.goBack();
			return true;
		} else {
			return false;
		}
	}

}
